const db = require("../../database");
const writeLogs = require('./writeLogs');

module.exports = (req, res) => {
    const username = req.auth.username;
    const sql = 'select status, verErrStartDate, friends, messages from users where username = ?';
    db.query(sql, username, (err, data) => {
        if (err) return res.cc(err)
        if (data.length === 0) return res.cc('用户名不存在')
        const {status, verErrStartDate, friends, messages} = data[0];
        if (status !== 0) {
            // 时间间隔大于1天
            if (Date.now() - verErrStartDate < 864e5) {
                return res.cc('账号已被封禁')
            } else {
                db.query(`update users set status = 0, chance = 3 where username = ${username}`, (err) => {
                    if (err) writeLogs('err', { err });
                })
            }
        }
        writeLogs('login', {username})
        res.cc({username, friends, messages}, 0);
    })
}